﻿using System;
using System.IO;
using FirstFloor.Documents.Controls.Commands;
using FirstFloor.Documents.IO;
using FirstFloor.Documents.IO.LibTiff;

namespace SampleBrowser.Commands
{
    /// <summary>
    /// The command that opens a JPEG, PNG or TIFF image from disk and assigns it to a DocumentDataSource.
    /// </summary>
    public class OpenImageFromDiskCommand
        : OpenFromDiskCommand
    {
        /// <summary>
        /// Initializes a new instance of the <see cref="OpenImageFromDiskCommand"/> class.
        /// </summary>
        public OpenImageFromDiskCommand()
        {
            this.Filter = "Images (*.jpg,*.png,*.tif)|*.jpg;*.png;*.tif|JPEG Images (*.jpg)|*.jpg|PNG Images (*.png)|*.png|TIFF Images (*.tif)|*.tif|All Files (*.*)|*.*";
        }

        /// <summary>
        /// Creates a package reader for given stream.
        /// </summary>
        /// <param name="stream">The stream.</param>
        /// <returns></returns>
        public override IPackageReader CreatePackageReader(Stream stream)
        {
            if (ImagePackageReader.IsJpegImage(stream) || ImagePackageReader.IsPngImage(stream)) {
                return new ImagePackageReader(stream);
            }
            if (TiffPackageReader.IsTiffImage(stream)) {
                return new TiffPackageReader(stream);
            }

            throw new ArgumentException("Not a valid image");
        }
    }
}
